# CMakeLists.txt
cmake_minimum_required (VERSION 3.5.1)
project (CineFormSDK)

# Build settings
option(BUILD_LIBS "Build codec libraries" ON)
option(BUILD_TOOLS "Build test tools" ON)
option(BUILD_STATIC "Build static component" ON)
option(BUILD_SEPARATED "Build a separate de/encoder libraries" OFF)

set(CMAKE_CONFIGURATION_TYPES "Debug;Release")
add_definitions(-D_ALLOCATOR=1 -DWARPSTUFF=1)

find_package(OpenMP)

if (WIN32)
	SET(COMPILER_FLAGS "")
endif (WIN32)

if (UNIX)
	SET(COMPILER_FLAGS -fPIC)
	SET(ADDITIONAL_LIBS "-luuid -lpthread")
	SET(TOY_LIBS "-lm")
endif (UNIX)

if (APPLE)
	SET(COMPILER_FLAGS -fvisibility=hidden)
	SET(ADDITIONAL_LIBS "-lpthread")
endif (APPLE)

if (OPENMP_FOUND)
	add_definitions(-DHAVE_OPENMP=1)
	SET(ADDITIONAL_LIBS "${ADDITIONAL_LIBS} ${OpenMP_CXX_FLAGS}")
endif (OPENMP_FOUND)

include_directories("Common" "Tables" "Codec" "ConvertLib" "WarpLib" "Example")
file(GLOB CODEC_SOURCES "Codec/*.c" "Codec/*.h" "Codec/*.cpp" "WarpLib/*.c" "WarpLib/*.h" )
file(GLOB ENCODER_SOURCES "EncoderSDK/*.cpp" "Common/*.h")
file(GLOB DECODER_SOURCES "DecoderSDK/*.cpp" "Common/*.h" "WarpLib/*.c" "WarpLib/*.h" "ConvertLib/*.cpp" "ConvertLib/*.h" )
file(GLOB EXAMPLE_SOURCE "Example/*.cpp" "Example/*.h" )
file(GLOB WAVELETDEMO_SOURCE "Example/WaveletDemo/*.c" "Example/WaveletDemo/*.h" )
file(GLOB PUBLIC_HEADERS "Common/*.h")

# Build CFHDCodec library (static and shared rules)
if (BUILD_LIBS)
	if (BUILD_STATIC)
		if (BUILD_SEPARATED)
			add_library(CFHDEncoderStatic STATIC ${CODEC_SOURCES} ${ENCODER_SOURCES})
			add_library(CFHDDecoderStatic STATIC ${CODEC_SOURCES} ${DECODER_SOURCES})
			target_compile_options(CFHDEncoderStatic PUBLIC ${COMPILER_FLAGS})
			target_compile_options(CFHDDecoderStatic PUBLIC ${COMPILER_FLAGS})		
			target_link_libraries (CFHDEncoderStatic)
			target_link_libraries (CFHDDecoderStatic)
		else (BUILD_SEPARATED)
			add_library(CFHDCodecStatic STATIC ${CODEC_SOURCES} ${ENCODER_SOURCES} ${DECODER_SOURCES})
			target_compile_options(CFHDCodecStatic PUBLIC ${COMPILER_FLAGS})
		
			set_target_properties(CFHDCodecStatic PROPERTIES POSITION_INDEPENDENT_CODE ON)
			if (UNIX)
				set_target_properties(CFHDCodecStatic PROPERTIES OUTPUT_NAME CFHDCodec)
			endif (UNIX)
		
			target_link_libraries(CFHDCodecStatic)
		endif (BUILD_SEPARATED)
	else (BUILD_STATIC)
		if (BUILD_SEPARATED)
			add_library(CFHDEncoder SHARED ${CODEC_SOURCES} ${ENCODER_SOURCES})
			add_library(CFHDDecoder SHARED ${CODEC_SOURCES} ${DECODER_SOURCES})
			target_compile_options(CFHDEncoder PUBLIC ${COMPILER_FLAGS})
			target_compile_options(CFHDDecoder PUBLIC ${COMPILER_FLAGS})
			target_compile_definitions(CFHDEncoder PUBLIC -DDYNAMICLIB=1)
			target_compile_definitions(CFHDDecoder PUBLIC -DDYNAMICLIB=1)
			target_link_libraries (CFHDEncoder)
			target_link_libraries (CFHDDecoder)
		else (BUILD_SEPARATED)
			add_library(CFHDCodecShared SHARED ${CODEC_SOURCES} ${ENCODER_SOURCES} ${DECODER_SOURCES})
			target_compile_options(CFHDCodecShared PUBLIC ${COMPILER_FLAGS})
			target_compile_definitions(CFHDCodecShared PUBLIC -DDYNAMICLIB=1 -DCODECCOMBINED=1)
			target_link_libraries(CFHDCodecShared)
			set_target_properties(CFHDCodecShared PROPERTIES POSITION_INDEPENDENT_CODE ON)
			set_target_properties(CFHDCodecShared PROPERTIES OUTPUT_NAME CFHDCodec)
		endif (BUILD_SEPARATED)
	endif (BUILD_STATIC)
endif (BUILD_LIBS)

# Build tools
if (BUILD_TOOLS)
    # TestCFHD
	add_executable(TestCFHD ${EXAMPLE_SOURCE})
	if (OPENMP_FOUND)
		target_compile_options(TestCFHD PRIVATE ${OpenMP_CXX_FLAGS})
	endif ()


    if (BUILD_STATIC)
		if (BUILD_SEPARATED)
			target_link_libraries(TestCFHD CFHDEncoderStatic CFHDDecoderStatic ${INTERNAL_LIBS} ${ADDITIONAL_LIBS})
		else (BUILD_SEPARATED)
			target_link_libraries(TestCFHD CFHDCodecStatic ${INTERNAL_LIBS} ${ADDITIONAL_LIBS})
		endif (BUILD_SEPARATED)
    else (BUILD_STATIC)
		if (BUILD_SEPARATED)
			target_link_libraries(TestCFHD CFHDEncoder CFHDDecoder  ${INTERNAL_LIBS} ${ADDITIONAL_LIBS})
		else (BUILD_SEPARATED)
			target_link_libraries(TestCFHD CFHDCodecShared ${INTERNAL_LIBS} ${ADDITIONAL_LIBS})
		endif (BUILD_SEPARATED)
    endif (BUILD_STATIC)

    # WaveletDemo
    add_executable(WaveletDemo ${WAVELETDEMO_SOURCE})
    target_link_libraries(WaveletDemo ${TOY_LIBS})
endif (BUILD_TOOLS)


# pkg-config integration
set(PROJECT_VERSION "10.0.2")
set(LIB_SUFFIX "" CACHE STRING "Define suffix of directory name (32/64)")
set(EXEC_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX} CACHE PATH "Installation prefix for executables and object code libraries" FORCE)
set(BIN_INSTALL_DIR ${EXEC_INSTALL_PREFIX}/bin CACHE PATH "Installation prefix for user executables" FORCE)
set(LIB_INSTALL_DIR ${EXEC_INSTALL_PREFIX}/lib${LIB_SUFFIX} CACHE PATH "Installation prefix for object code libraries" FORCE)
set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/include/cineformsdk CACHE PATH "Installation prefix for header files" FORCE)

# System wide installation
if (BUILD_STATIC)
	if (BUILD_SEPARATED)
		set(TARGET_NAMES CFHDEncoderStatic CFHDDecoderStatic)
	else (BUILD_SEPARATED)
		set(TARGET_NAMES CFHDCodecStatic)
	endif (BUILD_SEPARATED)
else (BUILD_STATIC)
	if (BUILD_SEPARATED)
		set(TARGET_NAMES CFHDEncoder CFHDDecoder)
	else (BUILD_SEPARATED)
		set(TARGET_NAMES CFHDCodecShared)
	endif (BUILD_SEPARATED)
endif (BUILD_STATIC)
foreach(TARGET_NAME ${TARGET_NAMES})
    set(LIB_INSTALL_NAMES ${LIB_INSTALL_NAMES} -l${TARGET_NAME})
endforeach()
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libcineformsdk.pc.cmake ${CMAKE_CURRENT_BINARY_DIR}/libcineformsdk.pc)

install(TARGETS ${TARGET_NAMES} DESTINATION lib/)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libcineformsdk.pc DESTINATION lib/pkgconfig)
install(FILES ${PUBLIC_HEADERS} DESTINATION ${INCLUDE_INSTALL_DIR})
